﻿@inject AppState AppState

<StackLayout Orientation="StackOrientation.Horizontal">
    <Switch @bind-IsToggled="ItemIsDone" />
    <Label Text="@Item.Text"
           TextDecorations="(Item.IsDone ? TextDecorations.Strikethrough : TextDecorations.None)"
           HorizontalOptions="LayoutOptions.FillAndExpand" />
    <Button Text="..."
            OnClick="OnDetailsClick" />
</StackLayout>

<ModalContainer @ref="todoEntryDialog" OnClosed="OnDetailsClosed">
    <TodoEntryDetails Item="Item" />
</ModalContainer>

@code
{
    [Parameter] public TodoItem Item { get; set; }

    ModalContainer todoEntryDialog;

    public void OnDetailsClick()
    {
        todoEntryDialog.ShowDialog();
    }

    // This intermediate property allows change notifications to be raised when the IsDone value is toggled
    bool ItemIsDone
    {
        get => Item.IsDone;
        set {
            if (value != Item.IsDone)
            {
                Item.IsDone = value;
                _ = AppState.TodoDatabase.SaveItemAsync(Item);
            }
        }
    }

    // NOTE: This method shouldn't be needed and is a temporary workaround
    public void OnDetailsClosed()
    {
        todoEntryDialog.FinishClosingDialog();
    }
}
